//
//  JAEmotionTool.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/14.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit

class JAEmotionTool: NSObject {

    static let shared: JAEmotionTool = JAEmotionTool()
    
    
    var dataSourceArr: [[[JAEmotionModel]]] {
        //1. 得到最近的二维数组
        let recentEmotions = devideEmotions(emotions: parseInfoPlist(path: defaultPath))
        
        //2. 得到默认的二维数组
        let defaultEmotions = devideEmotions(emotions: parseInfoPlist(path: defaultPath))
        
        //3. emoji
        let emojiEmotions = devideEmotions(emotions: parseInfoPlist(path: emojiPath))
        
        //4. lxh
        let lxhEmotions = devideEmotions(emotions: parseInfoPlist(path: lxhPath))
        
        return [recentEmotions, defaultEmotions, emojiEmotions, lxhEmotions]
    }
    
    /// 表情包的路径
    var bundlePath: String {
        return Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
    }
    
    /// 默认表情的info.plist的路径
    var defaultPath: String {
        return bundlePath + "/Contents/Resources/default/info.plist"
    }
    
    /// emoji表情的info.plist的路径
    var emojiPath: String {
        return bundlePath + "/Contents/Resources/emoji/info.plist"
    }
    
    /// lxh表情的info.plist的路径
    var lxhPath: String {
        return bundlePath + "/Contents/Resources/lxh/info.plist"
    }
    
    
    /// 从info.plist中解析表情
    ///
    /// - Parameter path: info.plist的路径
    /// - Returns: 对应的表情的模型数组
    func parseInfoPlist(path: String) -> [JAEmotionModel] {
        //模型数组
        var emotionModelsArr: [JAEmotionModel] = []
        
        //模型数组转字典数组
        if let dicArry = NSArray(contentsOfFile: path) as? [[String: Any]] {
            emotionModelsArr = NSArray.yy_modelArray(with: JAEmotionModel.self, json: dicArry) as! [JAEmotionModel]
            
            for model in emotionModelsArr {
                if model.type == 0{
                    model.fullPngPath = (path as NSString).deletingLastPathComponent + "/" + model.png!
                }
            }
        }
        
        return emotionModelsArr
    }
    
    /// 生一个section的数据源数组: 将某种表情按照cell进行分组, 20个一组, 如果不足20个,单独分一组
    ///
    /// - Parameter emotions: 一个secion的所有表情的模型
    /// - Returns: 按cell分好组的表情的模型数组(二维数组)
    func devideEmotions(emotions:[JAEmotionModel]) -> [[JAEmotionModel]] {
        //一共要几个cell来显示
        let pageCount = (emotions.count - 1)/20 + 1
        
        //创建一个空的二维数组
        var devidedEmotions: [[JAEmotionModel]] = []
        //range的起始位置
        var location = 0
        //range的长度
        var length = 20
        
        //遍历获取各个cell的模型数组
        for i in 0..<pageCount {
            //判断要载取的那一页的表情是否满20, 如果不满20个, 有多个显示多少个
            if emotions.count - i * length < length {
                length = emotions.count - i * length
            }
            
            //每一页cell的的表情的range
            let range = NSMakeRange(location, length)
            
            //从大数组中截取一个cell的表情
            let emotionsPerCell = (emotions as NSArray).subarray(with: range) as! [JAEmotionModel]
            
            //将截取的表情数组放到二维数组中
            devidedEmotions.append(emotionsPerCell)
            location += 20
        }
        
        return devidedEmotions
    }

}
